package com.ruijie.acl.web;

import java.util.ArrayList;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.jsf.ListDataModel;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclDevice;
import com.ruijie.acl.AclDeviceAccessGroup;
import com.ruijie.acl.service.AccessGroupService;
import com.ruijie.acl.service.DeviceAccessGroupService;
import com.ruijie.emp.device.Device;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;

/**
 * <p>
 * Title: ACL组与ACL设备关联管理Action类

 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 
 * </p>
 * <p>
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * @author ASAI
 */
@Name("deviceAccessGroupAction")
@Scope(ScopeType.CONVERSATION)
public class DeviceAccessGroupAction extends AclPlanBaseAction {

	@In("#{deviceAccessGroupService}")
	private DeviceAccessGroupService deviceAccessGroupService;
	
	@In("#{accessGroupService}")
	private AccessGroupService accessGroupService;
	
	private ListDataModel aclDeviceEntityModelList = null;
	
	private ListDataModel accessGroupEntityModelList = null;
	
	//ACL设备信息
	private AclDevice aclDevice;
	private Long deviceId = null;
	//ACl组信息
	private AccessGroup accessGroup;
	private Long groupId = null;
	
	private String differenceObject = new String();
	
	/**
	 * 取得ACL设备分页信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ListDataModel getAclDeviceEntityModelList(AccessGroup accessGroup) {
		differenceObject = AccessGroup.class.getName();
		if (accessGroup != null) {
			this.accessGroup = accessGroup;
			//if (aclDeviceEntityModelList == null || accessGroup.getId() != this.groupId) {
				currentPage = deviceAccessGroupService.pageQueryByAccessGroup(
						accessGroup, page, pageSize);
				aclDeviceEntityModelList = new ListDataModel(
						(List<AclDeviceAccessGroup>) currentPage.getResult());
			//}
			this.groupId = accessGroup.getId();
		}
		return aclDeviceEntityModelList;
	}

	/**
	 * 取得ACL组分页信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ListDataModel getAccessGroupEntityModelList(AclDevice aclDevice) {
		differenceObject = AclDevice.class.getName();
		if(aclDevice != null) {
			this.aclDevice = aclDevice;
			//if(accessGroupEntityModelList == null || aclDevice.getId() != this.deviceId) {
				currentPage = deviceAccessGroupService.pageQueryByAclDevice(
						aclDevice,null, page, pageSize);
				accessGroupEntityModelList = new ListDataModel(
						(List<AclDeviceAccessGroup>) currentPage.getResult());
			//}
			this.deviceId = aclDevice.getId();
		}
		
		return accessGroupEntityModelList;
	}
	
	
	public void delete(AclDeviceAccessGroup entity) {
		deviceAccessGroupService.remove(entity);
		AccessGroup accessGroupDevice = accessGroupService.getByNameWithAclDevice(entity.getAccessGroup().getName(), entity.getAclDevice());
		if (accessGroupDevice != null){
			accessGroupService.remove(accessGroupDevice);
		}
		updateModel();
	}
	
	@Override
	protected void updateModel() {
		if(differenceObject.equals(AccessGroup.class.getName())) {
			this.aclDeviceEntityModelList = null;
			getAclDeviceEntityModelList(accessGroup);
		}else if(differenceObject.equals(AclDevice.class.getName())) {
			this.accessGroupEntityModelList = null;
			getAccessGroupEntityModelList(aclDevice);
		}else{
			this.aclDeviceEntityModelList = null;
			getAclDeviceEntityModelList(accessGroup);
			this.accessGroupEntityModelList = null;
			getAccessGroupEntityModelList(aclDevice);
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	protected Repository getRepository() {
		return deviceAccessGroupService;
	}

	@Override
	protected String getAddPage() {
		return null;
	}

	@Override
	public EntityObject getCriteria() {
		return null;
	}

	@Override
	protected String getDetailPage() {
		return null;
	}

	@Override
	protected String getListPage() {
		return null;
	}

	@Override
	public EntityObject getModel() {
		return null;
	}

	@Override
	protected String getUpdatePage() {
		return null;
	}

	@Override
	public void resetModel() {

	}

	@Override
	public void setCriteria(EntityObject criteria) {

	}

	@Override
	public void setModel(EntityObject model) {


	}

	public AclDevice getAclDevice() {
		return aclDevice;
	}

	public void setAclDevice(AclDevice aclDevice) {
		this.aclDevice = aclDevice;
	}

	public Long getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(Long deviceId) {
		this.deviceId = deviceId;
	}

	public AccessGroup getAccessGroup() {
		return accessGroup;
	}

	public void setAccessGroup(AccessGroup accessGroup) {
		this.accessGroup = accessGroup;
	}

	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}
	/**
	 * 根据ACL组取得ACL设备列表
	 * @param entity ACL组
	 * @return ACL设备列表
	 */
	public List<AclDevice> getAclDeviceByAccessGroup(AccessGroup entity){
		List<AclDeviceAccessGroup> list = this.deviceAccessGroupService.getByAccessGroup(entity);
		List<AclDevice> aclDeviceList = new ArrayList<AclDevice>();
		if (list != null && list.size() > 0){
			for(AclDeviceAccessGroup aclDeviceAccessGroup : list){
				aclDeviceList.add(aclDeviceAccessGroup.getAclDevice());
			}
		}
		return aclDeviceList;
	}
	/**
	 * 根据ACL设备取得ACL组列表
	 * @param entity ACL设备
	 * @return ACL组列表
	 */
	public List<AccessGroup> getAclDeviceByAclDevice(AclDevice entity){
		List<AclDeviceAccessGroup> list = this.deviceAccessGroupService.getByAclDevice(entity);
		List<AccessGroup> accessGroupList = new ArrayList<AccessGroup>();
		if (list != null && list.size() > 0){
			for(AclDeviceAccessGroup aclDeviceAccessGroup : list){
				accessGroupList.add(aclDeviceAccessGroup.getAccessGroup());
			}
		}
		return accessGroupList;
	}
	
	/**
	 * ACL组详细界面的单设备部署

	 * @param id
	 * @param device
	 */
	public void reConfig(String id,Device device){		
		AccessGroup group = null;
		try {
			group = accessGroupService.get(Long.valueOf(id));
			accessGroupService.reConfig(group, device, "modify");
		} catch (BusinessException e) {
			throw e;
		}
		updateModel();
	}
	
	/**
	 * ACL组详细界面的删除设备

	 * @param id
	 * @param device
	 */
	public void deleteDeviceConfig(String id,Device device,AclDeviceAccessGroup entity){		
		AccessGroup group = null;
		try {
			group = accessGroupService.get(Long.valueOf(id));
			accessGroupService.reConfig(group, device, "DeleteAccessGroup");
		} catch (BusinessException e) {
			throw e;
		}
		this.delete(entity);
		updateModel();
	}

	public ListDataModel getAclDeviceEntityModelList() {
		return aclDeviceEntityModelList;
	}

	public void setAclDeviceEntityModelList(ListDataModel aclDeviceEntityModelList) {
		this.aclDeviceEntityModelList = aclDeviceEntityModelList;
	}

	public ListDataModel getAccessGroupEntityModelList() {
		return accessGroupEntityModelList;
	}

	public void setAccessGroupEntityModelList(
			ListDataModel accessGroupEntityModelList) {
		this.accessGroupEntityModelList = accessGroupEntityModelList;
	}
}
